package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbee.smartcommunities.entity.Cart;
import com.fbee.smartcommunities.entity.Goods;
import com.fbee.smartcommunities.mapper.CartMapper;
import com.fbee.smartcommunities.mapper.GoodsMapper;
import com.fbee.smartcommunities.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-26
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Boolean addGoodToCart(Long goodId, Long openId, Integer count) {
        Cart good = getOne(new QueryWrapper<Cart>().eq("user_id",openId).eq("goods_id", goodId).eq("state",0));
        if(good != null){
            cartMapper.update(null,new UpdateWrapper<Cart>().eq("user_id",openId).eq("goods_id", goodId).set("count",good.getCount() + count));
        } else {
            Cart cart = new Cart(goodId, openId, count, LocalDateTime.now());
            cartMapper.insert(cart);
        }
        return true;
    }

    @Override
    public List<Cart> getCartByUid(Long uid) {
        return cartMapper.getCartByUid(uid);
    }

}
